package mobile.pubcrawler.ui;

import java.util.ArrayList;

import mobile.pubcrawler.Filter;
import mobile.pubcrawler.PubCrawlerUI;
import mobile.pubcrawler.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Settings extends Activity {

	private Filter currentFilter;
	private ArrayList<String> categories;
	private int radius;
	private int progress;
	private TextView radiusSelected;
	
	private static String PUB = "Pubs";
	private static String RESTAURANT = "Restaurant";
	private static String CAFE = "Cafe";
	private static String CLUB = "Club";
	private static String BEER = "Biergarten";
	private static String COCKTAIL = "Cocktailbar";

	private int pub = 1;
	private int restaurant = 2;
	private int cafe = 3;
	private int club = 4;
	private int beer = 5;
	private int cocktail = 6;

	private boolean pubFilterSelected;
	private boolean restaurantFilterSelected;
	private boolean cafeFilterSelected;
	private boolean clubFilterSelected;
	private boolean beerFilterSelected;
	private boolean cocktailFilterSelected;
	
	private Settings activity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.menu_settings);
		setContentView(R.layout.settings);
		this.activity = this;
		
		// get a current instance of the filter
		currentFilter = Filter.getInstance();
		categories = currentFilter.getCategories();
		radius = currentFilter.getSearchRadius();
		
		progress = radius/100;	
		
		radiusSelected = (TextView)findViewById(R.id.searchRadius);
    	setRadiusSelected(radius);
		
		// set ImageButtons
		final ImageButton pubButton = (ImageButton) this
				.findViewById(R.id.togglePubSetting);
		pubButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				pubFilterSelected = !pubFilterSelected;
				refreshButtonIcons(pub, pubButton);
				if (pubFilterSelected) {
					if (!categories.contains(PUB))
						categories.add(PUB);
				} else {
					if (categories.contains(PUB))
						categories.remove(PUB);
				}
			}
		});

		final ImageButton restaurantButton = (ImageButton) this
				.findViewById(R.id.toggleRestaurantSetting);
		restaurantButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				restaurantFilterSelected = !restaurantFilterSelected;
				refreshButtonIcons(restaurant, restaurantButton);
				if (restaurantFilterSelected) {
					if (!categories.contains(RESTAURANT))
						categories.add(RESTAURANT);
				} else {
					if (categories.contains(RESTAURANT))
						categories.remove(RESTAURANT);
				}
			}
		});

		final ImageButton cafeButton = (ImageButton) this
				.findViewById(R.id.toggleCafeSetting);
		cafeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				cafeFilterSelected = !cafeFilterSelected;
				refreshButtonIcons(cafe, cafeButton);
				if (cafeFilterSelected) {
					if (!categories.contains(CAFE))
						categories.add(CAFE);
				} else {
					if (categories.contains(CAFE))
						categories.remove(CAFE);
				}
			}
		});

		final ImageButton clubButton = (ImageButton) this
				.findViewById(R.id.toggleClubSetting);
		clubButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				clubFilterSelected = !clubFilterSelected;
				refreshButtonIcons(club, clubButton);
				if (clubFilterSelected) {
					if (!categories.contains(CLUB))
						categories.add(CLUB);
				} else {
					if (categories.contains(CLUB))
						categories.remove(CLUB);
				}
			}
		});

		final ImageButton beerButton = (ImageButton) this
				.findViewById(R.id.toggleBeerSetting);
		beerButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				beerFilterSelected = !beerFilterSelected;
				refreshButtonIcons(beer, beerButton);
				if (beerFilterSelected) {
					if (!categories.contains(BEER))
						categories.add(BEER);
				} else {
					if (categories.contains(BEER))
						categories.remove(BEER);
				}
			}
		});

		final ImageButton cocktailButton = (ImageButton) this
				.findViewById(R.id.toggleCocktailSetting);
		cocktailButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				cocktailFilterSelected = !cocktailFilterSelected;
				refreshButtonIcons(cocktail, cocktailButton);
				if (cocktailFilterSelected) {
					if (!categories.contains(COCKTAIL))
						categories.add(COCKTAIL);
				} else {
					if (categories.contains(COCKTAIL))
						categories.remove(COCKTAIL);
				}
			}
		});
		// end ImageButtons

		// -----------set filters----------------------------------------------

		pubFilterSelected = currentFilter.getCategories().contains(PUB);
		if (pubFilterSelected)
			pubButton.setImageResource(R.drawable.pubfilter_selected_big);
		else
			pubButton.setImageResource(R.drawable.pubfilter_deselected_big);

		restaurantFilterSelected = currentFilter.getCategories().contains(
				RESTAURANT);
		if (restaurantFilterSelected)
			restaurantButton
					.setImageResource(R.drawable.restaurant_filter_selected);
		else
			restaurantButton
					.setImageResource(R.drawable.restaurant_filter_deselected);

		cafeFilterSelected = currentFilter.getCategories().contains(CAFE);
		if (cafeFilterSelected)
			cafeButton.setImageResource(R.drawable.coffee_filter_selected);
		else
			cafeButton.setImageResource(R.drawable.coffee_filter_deselected);

		clubFilterSelected = currentFilter.getCategories().contains(CLUB);
		if (clubFilterSelected)
			clubButton.setImageResource(R.drawable.club_filter_selected);
		else
			clubButton.setImageResource(R.drawable.club_filter_deselected);

		beerFilterSelected = currentFilter.getCategories().contains(BEER);
		if (beerFilterSelected)
			beerButton.setImageResource(R.drawable.beer_filter_selected);
		else
			beerButton.setImageResource(R.drawable.beer_filter_deselected);

		cocktailFilterSelected = currentFilter.getCategories().contains(
				COCKTAIL);
		if (cocktailFilterSelected)
			cocktailButton
					.setImageResource(R.drawable.cocktail_filter_selected);
		else
			cocktailButton
					.setImageResource(R.drawable.cocktail_filter_deselected);

		// -------------end set filters----------------------------------------

		SeekBar radiusSeekBar = (SeekBar) findViewById(R.id.seekBar);
		radiusSeekBar.setOnSeekBarChangeListener(radiusSeekBarChangeListener);
		radiusSeekBar.setProgress(progress);

		Button saveButton = (Button) this.findViewById(R.id.settingsSaveButton);
		saveButton.setOnClickListener(saveSettingsListener);
		
	} // end of OnCreate

	
	public void setRadiusSelected(int count) {
    	radiusSelected.setText(getString(R.string.seekbarTitle) +"  "+ String.valueOf(count) +"m");
    }
	
	private OnSeekBarChangeListener radiusSeekBarChangeListener = new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar seekBar) {setRadiusSelected(radius);}

		public void onStartTrackingTouch(SeekBar seekBar) {}

		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			if (progress <= 10)
				radius = 1000;
			else if ((progress > 11) && (progress <= 20))
				radius = 2000;
			else if ((progress > 21) && (progress <= 30))
				radius = 3000;
			else if ((progress > 31) && (progress <= 40))
				radius = 4000;
			else if ((progress > 41) && (progress <= 50))
				radius = 5000;
			else if ((progress > 51) && (progress <= 60))
				radius = 6000;
			else if ((progress > 61) && (progress <= 70))
				radius = 7000;
			else if ((progress > 71) && (progress <= 80))
				radius = 8000;
			else if ((progress > 81) && (progress <= 90))
				radius = 9000;
			else
				radius = 10000;
		}
	};

	private OnClickListener saveSettingsListener = new OnClickListener() {
		public void onClick(View v) {
			currentFilter.setCategories(categories);
			currentFilter.setSearchRadius(radius);
			// TODO if there is time a status would be nice, e.g. filters are changed now...
			currentFilter.saveFilterSettings(activity.getApplicationContext());
			activity.finish();
		}
	};

	// only responsible to set the correct icons on the buttons
	private void refreshButtonIcons(int specify, ImageButton aButton) {

		switch (specify) {
		case 1:
			if (pubFilterSelected)
				aButton.setImageResource(R.drawable.pubfilter_selected_big);
			else
				aButton.setImageResource(R.drawable.pubfilter_deselected_big);
			break;

		case 2:
			if (restaurantFilterSelected)
				aButton.setImageResource(R.drawable.restaurant_filter_selected);
			else
				aButton
						.setImageResource(R.drawable.restaurant_filter_deselected);
			break;

		case 3:
			if (cafeFilterSelected)
				aButton.setImageResource(R.drawable.coffee_filter_selected);
			else
				aButton.setImageResource(R.drawable.coffee_filter_deselected);
			break;

		case 4:
			if (clubFilterSelected)
				aButton.setImageResource(R.drawable.club_filter_selected);
			else
				aButton.setImageResource(R.drawable.club_filter_deselected);
			break;

		case 5:
			if (beerFilterSelected)
				aButton.setImageResource(R.drawable.beer_filter_selected);
			else
				aButton.setImageResource(R.drawable.beer_filter_deselected);
			break;

		case 6:
			if (cocktailFilterSelected)
				aButton.setImageResource(R.drawable.cocktail_filter_selected);
			else
				aButton.setImageResource(R.drawable.cocktail_filter_deselected);
			break;

		default:
			System.out.println("Wrong specifier!");
		}
	}

	// ----------------- Menu inflaters and listeners -------------------
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuMainMenu:
			PubCrawlerUI.getInstance().startMainUI();
			return true;
		case R.id.menuSettings:
			PubCrawlerUI.getInstance().startSettingsUI();
			return true;
		case R.id.menuQuit:
			System.exit(0);
			return true;
		}
		return false;
	}

}
